﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GMUtilitiesDotNet.ViewModels;
using ThouShaltNotBrickLevelEditor.Code.Managers;
using System.ComponentModel;
using GMUtilitiesDotNet.Helpers;

namespace ThouShaltNotBrickLevelEditor.ViewModels
{
    public sealed class MainWindowViewModel : ViewModelBase
    {
        #region Properties
        #region MainViewModel
        private MainViewModel mMainViewModel;
        public static readonly PropertyChangedEventArgs MainViewModelEventArgs = ObservableHelper.CreateArgs<MainWindowViewModel>(x => x.MainViewModel);

        /// <summary>
        /// MainViewModel property.
        /// </summary>
        public MainViewModel MainViewModel
        {
            get { return mMainViewModel; }
            private set
            {
                if (mMainViewModel != value)
                {
                    mMainViewModel = value;
                    RaisePropertyChangedEvent(MainViewModelEventArgs);
                }
            }
        }
        #endregion

        private CommandsManager mCommandsManager;
        private LevelManager mLevelManager;
        #endregion

        #region Events
        #region RequestCloseMainWindow
        public event EventHandler RequestCloseMainWindow;
        private void RaiseRequestCloseMainWindowEvent()
        {
            var handler = RequestCloseMainWindow;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }
        #endregion
        #endregion

        #region Constructors
        public MainWindowViewModel(LevelEditorManager pLevelEditorManager)
        {
            mCommandsManager = pLevelEditorManager.CommandsManager;
            mCommandsManager.CloseMainWindowsRequested += CloseMainWindowsRequestedHandler;

            mLevelManager = pLevelEditorManager.LevelManager;

            MainViewModel = new MainViewModel(pLevelEditorManager);
        }
        #endregion

        #region Overriden Methods
        protected override void OnDisposeManaged()
        {
            mCommandsManager.CloseMainWindowsRequested -= CloseMainWindowsRequestedHandler;

            base.OnDisposeManaged();
        }
        #endregion

        #region Methods
        public bool IsPendingLevelChanges()
        {
            return mLevelManager.IsPendingLevelChanges;
        }
        #endregion

        #region Event Handlers
        private void CloseMainWindowsRequestedHandler(object sender, EventArgs e)
        {
            RaiseRequestCloseMainWindowEvent();
        }
        #endregion
    }
}